package com.example.personnel_management_system.controller;

import com.example.personnel_management_system.entity.Institution;
import com.example.personnel_management_system.service.IInstitutionService;
import com.example.personnel_management_system.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("institution")
public class InstitutionController {
    @Autowired
    private IInstitutionService iInstitutionService;
    @RequestMapping("addInstitution")
    public Result<String>addInstitution(Institution institution){
        try {
            iInstitutionService.insert(institution);
            return Result.success("添加成功");
        }catch (Exception e){
            return Result.failure("添加机构错误");
        }
    }
    @RequestMapping("findall")
    public  Result<List<Institution>>findall(){
        return Result.success(iInstitutionService.findall());
    }
    @RequestMapping("findByIid")
    public  Result<Institution>findByIid(Integer iID){
        return Result.success(iInstitutionService.findByIid(iID));
    }
    @RequestMapping("findByParent")
    public  Result<List<Institution>>findByParent(Integer iParent){
        return Result.success(iInstitutionService.findByParent(iParent));
    }
    @RequestMapping("update")
    public Result<Void>update(Institution institution){
        try {
            iInstitutionService.update(institution);
            return Result.success();
        }catch (Exception e){
            return Result.failure();
        }
    }
}
